package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.extension.api.R;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StatusConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DomainEntity;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.dto.DomainDTO;
import com.ruoyi.system.domain.dto.DomainLandingDTO;
import com.ruoyi.system.domain.dto.DomainLinkDTO;
import com.ruoyi.system.service.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Anonymous
@RestController
@RequestMapping("domainapi")
public class AppDomainController extends BaseController {

    @Autowired
    private DomainService domainService;

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageUtils.startPage();
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody DomainLinkDTO domain) {
        return toAjax(domainService.insertDomain(domain));
    }

    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody DomainLinkDTO domain) {
        return toAjax(domainService.updateDomain(domain));
    }

    @GetMapping("/list")
    public TableDataInfo list() throws Exception {
        startPage();
        List<DomainEntity> list = domainService.queryAllDomainList();
        return getDataTable(list);

    }

    @GetMapping("/get")
    public AjaxResult get(@RequestParam Long domainId) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        DomainEntity domain = domainService.selectDomainById(domainId);
        DomainLinkDTO domainLinkDTO = new DomainLinkDTO();
        if(domain != null){
            domainLinkDTO.setId(domain.getId());
            domainLinkDTO.setDomain(domain.getDomain());
            domainLinkDTO.setStatus(domain.getStatus());
            if(domain.getParentId() == null || domain.getParentId()==0){
                List<DomainEntity> domainLinks = domainService.queryDomainListByParentId(domainId);
                if(!domainLinks.isEmpty()){
                    List<DomainDTO> domainLinkList = new ArrayList<>();
                    for (DomainEntity domainLink : domainLinks) {
                        DomainDTO domainDTO = new DomainDTO();
                        domainDTO.setId(domainLink.getId());
                        domainDTO.setDomain(domainLink.getDomain());
                        domainDTO.setStatus(domainLink.getStatus());

                        Long linkId = domainLink.getId();
                        List<DomainEntity> domainPages = domainService.queryDomainListByParentId(linkId);
                        List<DomainLandingDTO> domainLandingPageList = new ArrayList<>();
                        for (DomainEntity domainPage : domainPages) {
                            DomainLandingDTO domainLandingDTO = new DomainLandingDTO();
                            domainLandingDTO.setId(domainPage.getId());
                            domainLandingDTO.setDomain(domainPage.getDomain());
                            domainLandingDTO.setStatus(domainPage.getStatus());
                            domainLandingPageList.add(domainLandingDTO);
                        }
                        domainDTO.setPages(domainLandingPageList);
                        domainLinkList.add(domainDTO);
                    }
                    domainLinkDTO.setLinks(domainLinkList);
                }
            }


        }

        ajax.put("domain", domainLinkDTO);
        return ajax;
    }

}
